package travlefate.story.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.joooonho.SelectableRoundedImageView;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import travlefate.story.R;
import travlefate.story.activity.StoryLookActivity;
import travlefate.story.bean.ResultComment;
import travlefate.story.bean.ResultStory;
import travlefate.story.utils.Http;
import travlefate.story.utils.Loger;
import travlefate.story.views.NoScrollGridView;

/**
 * @author keven
 * @data 15/12/28 下午4:15
 * @package travlefate.story.adapter
 */

public class StoryInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_INFO = 0, TYPE_COMMENTS = 1, TYPE_FOOT = 2;
    private static final String TAG = "StoryInfoAdapter";
    private final ResultStory story;
    private final Context context;
    private int single_width = 0;
    private double zoom_x1 = 0.65, zoom_x2 = 0.45;
    private List<ResultComment> comments = new ArrayList<>();

    public StoryInfoAdapter(Context context, ResultStory story) {
        this.context = context;
        this.story = story;
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        single_width = displayMetrics.widthPixels;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder = null;
        View view = null;
        switch (viewType) {
            case TYPE_INFO:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.type_story_info, null);
                viewHolder = new InfoHolder(view);
                break;
            case TYPE_FOOT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.type_foot, null);
                viewHolder = new FootHolder(view);
                break;
            case TYPE_COMMENTS:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.type_comment, null);
                viewHolder = new CommentHolder(view);
                break;
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        switch (getItemViewType(position)) {
            case TYPE_INFO:
                ResultStory.UserEntity user = story.getUser();
                InfoHolder infoHolder = (InfoHolder) holder;
                infoHolder.story_city.setText(TextUtils.isEmpty(story.getCity()) ? "" : story.getCity());
                infoHolder.story_text.setText(TextUtils.isEmpty(story.getStory_info()) ? "" : story.getStory_info());
                infoHolder.story_nikename.setText(TextUtils.isEmpty(user.getUsername()) ? "" : user.getUsername());

                //设置发布时间
                String story_time = story.getStory_time();
                if (story_time != null) {
                    story_time += "000";
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = new Date();
                    date.setTime(Long.valueOf(story_time));
                    infoHolder.story_send_time.setText(sdf.format(date));
                } else {
                    infoHolder.story_send_time.setText("");
                }

                //设置性别
                if (user.getUsersex() == null) {

                } else if (user.getUsersex().equals("0")) {
                    infoHolder.story_sex.setImageResource(R.mipmap.ic_sex_man);
                } else {
                    infoHolder.story_sex.setImageResource(R.mipmap.ic_sex_woman);
                }

                //设置头像
                Picasso.with(context).load(Http.BASE_PORTRAIT + user.getPortrait()).resizeDimen(R.dimen.item_portrait_width_height, R.dimen.item_portrait_width_height).centerCrop().into(infoHolder.story_portrait);

                List<String> pics = story.getPics();

                if (pics != null && !pics.isEmpty() && pics.size() == 1) {

                    //加载单图
                    infoHolder.story_single_image.setVisibility(View.VISIBLE);
                    infoHolder.info_images.setVisibility(View.GONE);
                    inflateSingleData(infoHolder, pics);

                } else if (pics != null && !pics.isEmpty()) {
                    //加载多图
                    infoHolder.story_single_image.setVisibility(View.GONE);
                    infoHolder.info_images.setVisibility(View.VISIBLE);
                    infoHolder.imgAdapter.addDatas(pics);
                    infoHolder.info_images.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(context, StoryLookActivity.class);
                            intent.putExtra("story", story);
                            intent.putExtra("check", position);
                            context.startActivity(intent);
                        }
                    });
                } else {
                    infoHolder.story_single_image.setVisibility(View.GONE);
                }
                break;
        }

    }

    /**
     * 加载单图
     *
     * @param itemHolder
     * @param pics
     */
    private void inflateSingleData(final InfoHolder itemHolder, List<String> pics) {
        final String url = Http.BASE_UPLOADS + pics.get(0);
        Picasso.with(context).load(url).into(itemHolder.story_single_image, new Callback() {
            @Override
            public void onSuccess() {
                BitmapDrawable drawable = (BitmapDrawable) itemHolder.story_single_image.getDrawable();
                Bitmap source = drawable.getBitmap();
                ViewGroup.LayoutParams params = itemHolder.story_single_image.getLayoutParams();
                //横图
                if (source.getWidth() > source.getHeight()) {
                    double single_imagewidth = single_width * zoom_x1;
                    double i = single_imagewidth / source.getWidth();
                    params.height = (int) (source.getHeight() * i);
                    params.width = (int) (source.getWidth() * i);
                } else {
                    //竖图
                    double single_imagewidth = single_width * zoom_x2;
                    double i = single_imagewidth / source.getWidth();
                    params.height = (int) (source.getHeight() * i);
                    params.width = (int) (source.getWidth() * i);
                }
            }

            @Override
            public void onError() {
                Loger.i(TAG, "loadImage:加载失败");
            }

        });


        itemHolder.story_single_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, StoryLookActivity.class);
                intent.putExtra("story", story);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return 2+comments.size()+5;
    }

    @Override
    public int getItemViewType(int position) {
        int type = 0;
        if (position == 0) {
            type = TYPE_INFO;
        }else if((position+1)==getItemCount()){
            type = TYPE_FOOT;
        }else{
            type = TYPE_COMMENTS;
        }

        return type;
    }

    class InfoHolder extends RecyclerView.ViewHolder {

        NoScrollGridView info_images;
        TextView story_text;  //故事文本
        TextView story_city;      //发布城市
        ImageView story_sex;      //发布人性别
        TextView story_nikename;  //发布人昵称
        TextView story_send_time; //发布时间
        SelectableRoundedImageView story_portrait;    //发布人头像
        ImageView story_single_image;       //单图图像
        private final StoryListImgAdapter imgAdapter;

        public InfoHolder(View itemView) {
            super(itemView);
            story_portrait = (SelectableRoundedImageView) itemView.findViewById(R.id.story_portrait);

            story_text = (TextView) itemView.findViewById(R.id.story_text);
            story_city = (TextView) itemView.findViewById(R.id.story_city);
            story_sex = (ImageView) itemView.findViewById(R.id.story_sex);
            story_nikename = (TextView) itemView.findViewById(R.id.story_nikename);
            story_send_time = (TextView) itemView.findViewById(R.id.story_send_time);

            story_single_image = (ImageView) itemView.findViewById(R.id.story_single_image);
            info_images = (NoScrollGridView) itemView.findViewById(R.id.info_images);

            imgAdapter = new StoryListImgAdapter();
            info_images.setAdapter(imgAdapter);

        }
    }

    /**
     * 点击加载更多
     */
    class FootHolder extends RecyclerView.ViewHolder {

        public FootHolder(View itemView) {
            super(itemView);
        }
    }

    /**
     * 评论专区
     */
    class CommentHolder extends RecyclerView.ViewHolder{

        public CommentHolder(View itemView) {
            super(itemView);
        }
    }

}
